﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TCHSHue.BLL;
using TCHSHue.DAL;

namespace TCHSHue.Modules.IndexControl.TraCuu
{
    public partial class ctrTraCuuHoSo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                NapDanhSachPhuong();
            }
            var mode = Request.QueryString["mode"];
            if (mode == "tracuuhoso")
            {
                var maBN = Request.QueryString["id"];
                var hoSo = HoSoBLL.getHoSo(maBN);
                if (hoSo != null)
                {
                    divThongTinChiTiet.Visible = true;
                    lbHoTen.Text = hoSo.hoTen;
                    lbPhuong.Text = hoSo.phuong;
                    lbSoBN.Text = hoSo.maBN;
                    lbSoTo.Text = hoSo.soTo;
                    lbSoThua.Text = hoSo.soThua;
                    var tinhTrang = TinhTrangHoSoBLL.getTinhTrang(hoSo.maTrangThai.HasValue ? hoSo.maTrangThai.Value : -1);
                    if (tinhTrang == null)
                        lbTinhTrang.Text = "Chưa cập nhật";
                    else
                        lbTinhTrang.Text = tinhTrang.noiDungTrangThai;
                }
            }
        }

        void NapDanhSachPhuong()
        {
            var ls = PhuongBLL.getList();
            var phuong = new Phuong() {tenPhuong="Tất cả" };
            ls.Insert(0, phuong);
            ddlPhuong.DataSource = ls;
            ddlPhuong.DataTextField = "tenPhuong";
            ddlPhuong.DataValueField = "tenPhuong";
            ddlPhuong.DataBind();
        }

        protected void RedirectEdit(object sender, CommandEventArgs e)
        {
            Response.Redirect("~/Modules/Index.aspx?mode=tracuuhoso&id=" + e.CommandArgument.ToString());
        }

        protected void bntTraCuu_Click(object sender, EventArgs e)
        {
            divThongTinChiTiet.Visible = false;
            divDanhsachketqua.Visible = true;
            thongBao.Visible = false;
            var hoTen = Server.HtmlEncode(txtHoTen.Text);
            var soBN = Server.HtmlEncode(txtSBN.Text);
            var soTo = Server.HtmlEncode(txtSoTo.Text);
            var soThua = Server.HtmlEncode(txtSoThua.Text);
            var phuong = ddlPhuong.SelectedValue;
            if (!string.IsNullOrEmpty(hoTen) || !string.IsNullOrEmpty(soBN) || !string.IsNullOrEmpty(soTo) || !string.IsNullOrEmpty(soThua))
            {
                var ls = HoSoBLL.getList(hoTen, soBN, soTo, soThua, phuong, 50, getPageCurrent());
                gridHoSo.DataSource = ls;
                gridHoSo.DataBind();
                //PhanTrang(HoSoBLL.getToTal(hoTen, soBN, soTo, soThua, phuong), getPageCurrent(), "../Modules/Index.aspx?");
            }
            else
                thongBao.Visible = true;
        }
        
        int getPageCurrent()
        {
            var pageIndex = Request.QueryString["pageIndex"];
            int temp;
            int pCurrent = 1;
            if (!int.TryParse(pageIndex, out temp))
                pCurrent = 1;
            else
                pCurrent = int.Parse(pageIndex);
            return pCurrent;
        }

        void PhanTrang(int total, int pCurrent, string baseLink)
        {
            Paging paGing = new Paging(Hang.PAGESIZE);
            string resultPaging = paGing.CreatePaging(total, pCurrent, baseLink);
            if (string.IsNullOrEmpty(resultPaging))
                divPaging.Visible = false;
            else
            {
                divPaging.InnerHtml = resultPaging;
            }
        }

    }
}